﻿using System;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Reviewer.Entities.Services;
using Reviewer.Business;
using Reviewer.Entities;
using System.Globalization;

public partial class public_html_NewSiteTest_Releaseliste : System.Web.UI.Page
{
    public string SpilID = "";
    public string Format = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!ClientScript.IsClientScriptIncludeRegistered("jquery-1.3.2.min.js"))
        //{
        //    ClientScript.RegisterClientScriptInclude("jquery-1.3.2.min.js", ResolveUrl("~/js/jquery-1.3.2.min.js"));
        //}

        //if (!ClientScript.IsClientScriptIncludeRegistered("jquery-ui-1.7.2.custom.min.js"))
        //{
        //    ClientScript.RegisterClientScriptInclude("jquery-ui-1.7.2.custom.min.js", ResolveUrl("~/js/jquery-ui-1.7.2.custom.min.js"));
        //}

        //if (!ClientScript.IsClientScriptIncludeRegistered("jquery.flip.min.js"))
        //{
        //    ClientScript.RegisterClientScriptInclude("jquery.flip.min.js", ResolveUrl("~/js/jquery.flip.min.js"));
        //}

        if (!IsPostBack)
        {
            Guid gameId = Guid.Empty;
            if (!String.IsNullOrEmpty(Request["GameId"]))
            {
                try
                {
                    gameId = new Guid(Request["GameId"]);
                }
                catch { }
            }

            int gameFormat = -1;
            if (!String.IsNullOrEmpty(Request["Format"]))
            {
                gameFormat = int.Parse(Request["Format"]);
            }

            #region old query method
            int id = -1;
            if (!String.IsNullOrEmpty(Request["Id"]))
            {
                id = int.Parse(Request["Id"]);
            }
            #endregion

            Game currentGame = null;
            Game[] currentGames = null;

            using (IMetadataService<Genre> genreService = ServiceFactory.GetMetadataService<Genre>())
            using (IMetadataService<Format> formatService = ServiceFactory.GetMetadataService<Format>())
            using (IGameService service = ServiceFactory.GameService)
            {
                #region Old query method
                if (id > 0)
                {
                    Game singleGame = service.GetBy(id);
                    if (singleGame != null)
                    {
                        gameId = singleGame.GameId;
                        gameFormat = singleGame.Format;
                    }
                }
                #endregion

                if (!gameId.Equals(Guid.Empty) && gameFormat > 0)
                {
                    currentGames = service.GetBy(gameId);
                    currentGame = currentGames.FirstOrDefault(t => t.Format.Equals(gameFormat));
                }

                if (currentGame == null)
                {
                    if (currentGames != null && currentGames.Length > 0)
                    {
                        currentGame = currentGames[0];
                    }
                    else
                    {
                        Response.Redirect(ResolveUrl("~/404.aspx"));
                        return;
                    }
                }

                #region info
                this.Title = "Spil Information: " + currentGame.Title + " til " + formatService.GetAll().FirstOrDefault(t => t.Id.Equals(currentGame.Format)).Name;

                lblOverskrift.Text = String.Format(CultureInfo.InvariantCulture, "{0} ({1})", currentGame.Title, formatService.GetAll().FirstOrDefault(t => t.Id.Equals(currentGame.Format)).Name);
                lblGenre.Text = genreService.GetAll().First(t => t.Id.Equals(currentGame.Genre)).Name;

                lblMulti.Text = HelperUtilities.GetMultiplayerInfoName(currentGame.MultiplayerInfo);
                lblUdgiver.Text = String.IsNullOrEmpty(currentGame.Publisher) ? "<i>Oplysning mangler...</i>" : currentGame.Publisher;
                lblUdvikler.Text = String.IsNullOrEmpty(currentGame.Developer) ? "<i>Oplysning mangler...</i>" : currentGame.Developer;

                lblDato.Text =
                    currentGame.ReleaseDate.Equals(DateTime.MinValue.Date) ? "<i>Ukendt dato</i>" :
                    currentGame.ReleaseDate.Equals(DateTime.MaxValue.Date) ? "<i>Kommer senere</i>" :
                    currentGame.ReleaseDate.Year.Equals(6) ? "<i>Aflyst</i>" : currentGame.ReleaseDate.ToLongDateString();
                #endregion

                #region topImage
                GameTopImage topImage = service.GetAssociatedTopImage(currentGame.GameId);
                if (topImage != null)
                {
                    DivTopBilled.Attributes.Add("style", "background-image:url(/SpilTop/" + topImage.FileName + "); width:600px; height:150px; overflow:hidden;");
                    lblOverskrift.Visible = false;

                    if (!topImage.LogoInText)
                    {
                        lblImg.Text = "&nbsp;" + currentGame.Title;
                        if (topImage.LogoTextColor.ToUpperInvariant().Equals("#FFFFFF", StringComparison.Ordinal))
                        {
                            lblImg.ForeColor = System.Drawing.Color.White;
                        }

                        if (topImage.LogoTextColor.ToUpperInvariant().Equals("#000000", StringComparison.Ordinal))
                        {
                            lblImg.ForeColor = System.Drawing.Color.White;
                        }
                    }
                }
                else
                {
                    DivTopBilled.Visible = false;
                }
                #endregion

                #region cover
                imgCover.ImageUrl = UrlUtillity.GetGameCoverUri(currentGame);
                #endregion

                #region other formats
                DivOther.InnerHtml += "&nbsp;&nbsp;";
                if (currentGames.Length > 1)
                {
                    bool first = true;
                    foreach (Game g in currentGames.Where(t => !t.Format.Equals(currentGame.Format)))
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            DivOther.InnerHtml += ", ";
                        }

                        DivOther.InnerHtml += String.Format(CultureInfo.InvariantCulture, "<a href=\"{0}\">{1}</a>"
                            , UrlUtillity.GetGameUri(g)
                            , formatService.GetAll().FirstOrDefault(t => t.Id.Equals(g.Format)).Name);
                    }
                    DivOther.InnerHtml += ".";
                }
                #endregion

                #region screenshots
                GameScreenshot[] screenshots = service.GetAssociatedScreenshots(currentGame.GameId);
                if (screenshots == null || screenshots.Length == 0)
                {
                    DivContentScreens.InnerHtml = "<center><i>p.t. er der desværre ingen Screenshots</i></center>";
                }
                else
                {
                    foreach (GameScreenshot shot in screenshots)
                    {
                        if (shot.Format != currentGame.Format)
                            continue;

                        DivContentScreens.InnerHtml +=
                            String.Format(CultureInfo.InvariantCulture, "<a href=\"/Screenshot.aspx?PicID={0}&SpilID={1}&Format={2}\"><img border=\"0\" style=\"padding:2px;\" width=\"140px\" src=\"/screenshots/{3}\"></a>"
                            , shot.Id.ToString()
                            , currentGame.GameId.ToString()
                            , shot.Format.ToString()
                            , shot.FileName);
                    }
                }
                #endregion

                #region video
                GameVideo video = service.GetAssociatedVideo(currentGame.GameId);
                if (video == null)
                {
                    DivTrailers.Visible = false;
                }
                else
                {
                    DivTrailersContent.InnerHtml += String.Format(CultureInfo.InvariantCulture, "<a href=\"{0}\">{1}</a>"
                        , UrlUtillity.GetGameVideoUri(video, currentGame)
                        , video.Title);
                }

                #endregion

                bool hasRelatedContent = false;

                using (IReviewService reviewService = ServiceFactory.ReviewService)
                {
                    Review[] reviews = reviewService.FindByGameId(currentGame.Id).OrderByDescending(t => t.Id).ToArray();
                    if (reviews.Length > 0)
                    {
                        hasRelatedContent = true;
                        foreach (Review review in reviews)
                        {
                            DivContentRelater.InnerHtml += String.Format("<i>{0}</i><br /><a href=\"{1}\"><b>{2} Anmeldelse</b></a><br /><br />"
                                , review.LastModifiedDate
                                , UrlUtillity.GetReviewUri(review)
                                , currentGame.Title);
                        }
                    }
                }

                using (INewsService newsService = ServiceFactory.NewsService)
                {
                    IOrderedEnumerable<News> newsItems = newsService.GetAllByGameRelation(currentGame.GameId).OrderByDescending(t => t.NewsDate);
                    if (newsItems.Count() > 0)
                    {
                        hasRelatedContent = true;
                        foreach (News item in newsItems)
                        {
                            DivContentRelater.InnerHtml += String.Format("<i>{0}</i><br /><a href=\"{1}\">{2}</a><br /><br />"
                                , item.NewsDate.ToShortDateString()
                                , UrlUtillity.GetNewsUri(item)
                                , item.Title);

                        }
                    }
                }

                if (!hasRelatedContent)
                {
                    DivContentRelater.InnerHtml = "<center><i>p.t. er der desværre intet relateret indhold</i></center>";
                }

                using (IGameViewService gameViews = ServiceFactory.GameViewService)
                {
                    gameViews.LogView(currentGame.Id, currentGame.GameId);
                }


                PEGIRating rating = service.GetRatings(currentGame.GameId).Where(t => t.Format.Equals(currentGame.Format)).FirstOrDefault();
                this.uiPnlPEGIRating.Visible = rating != null;
                if (rating != null)
                {
                    this.PEGI3.Visible = rating.AgeRating.Equals(3);
                    this.PEGI7.Visible = rating.AgeRating.Equals(7);
                    this.PEGI12.Visible = rating.AgeRating.Equals(12);
                    this.PEGI16.Visible = rating.AgeRating.Equals(16);
                    this.PEGI18.Visible = rating.AgeRating.Equals(18);

                    this.PEGILang.Visible = rating.IsRatedBadLanguage;
                    this.PEGIDisc.Visible = rating.IsRatedDiscrimination;
                    this.PEGIDrugs.Visible = rating.IsRatedDrugs;
                    this.PEGIFear.Visible = rating.IsRatedFear;
                    this.PEGIGamb.Visible = rating.IsRatedGambling;
                    this.PEGIOnline.Visible = rating.IsRatedOnline;
                    this.PEGISex.Visible = rating.IsRatedSex;
                    this.PEGIViol.Visible = rating.IsRatedViolence;
                }
            }
        }
    }
}
